<?php

class Ead_MinhacontaController extends Ead_Controller_Action
{
    
    private $_table     = 'ALUNO';
    private $_pk        = 'ALU_ID';
    private $_order_col = 'a.ALU_NOM';
    private $_order     = 'ASC';
    private $_sta_col   = 'ALU_STA';
    private $obj        = '';
    private $form       = '';
    public  $dir        = 'public/upload/aluno/';
    public  $_file_col  = 'ALU_FILE';
    
    public function init()
    {
    
    }

    public function indexAction() {
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/validation/dist/jquery.validate.min.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/ead/minhaconta.js?1=2');
        
        if($this->getRequest()->isPost()) {
                        
            $formData = $this->getRequest()->getPost();
            
            $senha =  sha1($formData['senha']);
            $nova_senha = sha1($formData['nova_senha']);
            $rep_senha = sha1($formData['rep_senha']);
            
            
            $query = 'SELECT SQL_CACHE USU_SENHA FROM USUARIOS WHERE USU_ID = '.$this->storage->read()->USU_ID;
            $usu = $this->db->fetchRow($query);
  
            /*
            if($senha != $usu['USU_SENHA']){
                echo '<script>alert("Senha antiga incorreta!");</script>';
            
            } else 
            */
            if($nova_senha != $rep_senha){
                echo '<script>alert("Senhas estão diferentes!");</script>';
            }else{
            
                $data = array(
                    'USU_SENHA' => $nova_senha,
                );
                
                $this->db->update('USUARIOS',$data,'USU_ID = '. $this->storage->read()->USU_ID);

                echo '<script>alert("Senha alterada com sucesso!");</script>';
            }
        }   
    }
    
    public function fotoAction() {
        
        Zend_Loader::loadClass('tools','library');
        Zend_Loader::loadClass('dominio','library');
        Zend_Loader::loadClass('imagem','library');
        
        Zend_Loader::loadClass('WideImage','library/wideimage/lib/');
        
        $sessao = new Zend_Session_Namespace('Aluno');
                
        if($this->getRequest()->isPost() && $_FILES[$this->_file_col]['tmp_name'] > '') { 
            $formData = $this->getRequest()->getPost();
   
            $id = (int) $formData[$this->_pk];//$this->form->getValue($this->_pk);
            
            //foreach (glob($this->dir.$id.'_*.*') as $file) unlink($file);
            //imagem::delete($id,$this->dir,TRUE);
            
            $imgs = $this->dir.$id.'_can.*'; 
            foreach (glob($imgs) as $src) unlink($src);
            
            
            $returnImg = imagem::upload($this->_file_col,$this->dir,$id.'_can');

            imagem::rename($id.'_can',$this->dir,$returnImg['filenew']);
            
            $imgs = $this->dir.$id.'_can.*'; 
            foreach (glob($imgs) as $src) {
                $img = $src;
                //imagem::reize($img, $img, '320', '240');
                //imagem::reize($img, str_replace($id,$id.'_det',$img), '240', '240');
                //imagem::reize($img, str_replace($id,$id.'_tmb',$img), '80', '95');
                //imagem::reize($img, str_replace($id,$id.'_crt',$img), '80', '95');
                imagem::reize($img, str_replace($id.'_can',$id.'_ead',$img), '60', '50');
                
                // _can
                $image = WideImage::load($img);
                $resized = $image->resize('320', '240', 'outside')->crop('50% - '.('320'/2), '50% - '.('240'/2), '320', '240');
                $filenewp = $img; 
                $resized->saveToFile($filenewp); 
                @chmod($img, 0755);
                
            }
            
            $this->_helper->redirector('foto');
            
        }else{
        
            $where = ' ALU_ID = '.$sessao->aluno['ALU_ID'];
            $query = 'SELECT SQL_CACHE * FROM '.$this->_table.' WHERE '.$where;
            $result = $this->db->fetchRow($query);
            $this->view->data = $result;
        }   
    }
    
    public function salvarfotoAction(){
        
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender();  
         
        // erro locaweb com imagecreatefromjpeg e não gravar foto, altera php.ini para allow_url_fopen = On
        
        $ok = FALSE;
        $img = '';
        $id = $this->_getParam('id', '0');
        
        if ($_POST['type'] == "pixel") {
            // input is in format 1,2,3...|1,2,3...|...
            $im = imagecreatetruecolor(240,240);

            foreach (explode("|", $_POST['image']) as $y => $csv) {
                foreach (explode(";", $csv) as $x => $color) {
                    imagesetpixel($im, $x, $y, $color);
                }
            }
        } else {
            
            Zend_Loader::loadClass('imagem','library');
            
            //foreach (glob($this->dir.$id.'_*.*') as $file) unlink($file);
            //imagem::delete($id,$this->dir,TRUE);
            foreach (glob($this->dir.$id.'_ead.*') as $file) unlink($file);
            foreach (glob($this->dir.$id.'_can.*') as $file) @unlink($file);
            
            // cria foto tumb do aluno 
            Zend_Loader::loadClass('WideImage','library/wideimage/lib/');
            
            
            // input is in format: data:image/png;base64,...
            $im = imagecreatefrompng($_POST['image']);
            //$ok = imagepng($im,$this->dir.$id.'.png');
            $ok = imagepng($im,$this->dir.$id.'_can.png');
            $img = '<img src="'.$this->_urlbase.'/'.$this->dir.$id.'_can.png?'.rand(100,10000).'" />';
            
            /*
            $image = WideImage::load($this->dir.$id.'.png');
            //$resized = $image->resize('70', '100', 'fill'); 
            $resized = $image->resize('240', '240', 'outside')->crop('50% - '.('240'/2), '50% - '.('240'/2), '240', '240');
            $filenewp = $this->dir.$id.'_det.png';
            $resized->saveToFile($filenewp);
            @chmod($filenewp, 0755); 
            
            $image = WideImage::load($this->dir.$id.'.png');
            //$resized = $image->resize('70', '100', 'fill'); 
            $resized = $image->resize('80', '95', 'outside')->crop('50% - '.('80'/2), '50% - '.('95'/2), '80', '95');
            $filenewp = $this->dir.$id.'_tmb.png'; 
            $resized->saveToFile($filenewp); 
            @chmod($filenewp, 0755);
            
            $image = WideImage::load($this->dir.$id.'.png');
            //$resized = $image->resize('70', '100', 'fill'); 
            $resized = $image->resize('80', '95', 'outside')->crop('50% - '.('80'/2), '50% - '.('95'/2), '80', '95');
            $filenewp = $this->dir.$id.'_crt.png'; 
            $resized->saveToFile($filenewp); 
            @chmod($filenewp, 0755);
            */
            
            $image = WideImage::load($this->dir.$id.'_can.png');
            $resized = $image->resize('320', '240', 'outside')->crop('50% - '.('320'/2), '50% - '.('240'/2), '320', '240');
            $filenewp = $this->dir.$id.'_can.png'; 
            $resized->saveToFile($filenewp); 
            @chmod($this->dir.$id.'_can.png', 0755);
            
            
            $image = WideImage::load($this->dir.$id.'_can.png');
            $resized = $image->resize('60', '50', 'outside')->crop('50% - '.('60'/2), '50% - '.('50'/2), '60', '95');
            $filenewp = $this->dir.$id.'_ead.png'; 
            $resized->saveToFile($filenewp); 
            @chmod($filenewp, 0755);
        }
        
        echo json_encode(array('ok' => $ok,'img' => $img));
    }
    
}